#include "udp.h"

extern void udp_main(const int fd,const char *argv);

int main(int argc,const char *argv[]){
	//获取环境变量
	char *port = getenv("DICTIONARY_SERVER_PORT");
	if(port == NULL){
		printf("没有发现环境变量[DICITONARY_SERVER_PORT]\n");
		exit(EXIT_FAILURE);
	}
	char *host = getenv("DICTIONARY_SERVER_HOST");
	if(host == NULL){
		printf("没有发现环境变量[DICITONARY_SERVER_HOST]\n");
		exit(EXIT_FAILURE);
	}
	//检查参数
	if(argc < 2){
		printf("[%s][word]\n",argv[0]);
		exit(EXIT_FAILURE);
	}
	//打印环境变量的值
	printf("服务器的主机IP是：%s\t,端口号是：%s\n",host,port);
	//创建套接字
	int fd = socket(AF_INET,SOCK_DGRAM,0);
	if(fd < 0){
		ErrExit("socket");
	}
	//设置通信结构体
	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(atoi(port));
	if(inet_aton(host,&addr.sin_addr) == 0){
		printf("[%s:%d] Invalid address\n",__FUNCTION__,__LINE__);
		exit(EXIT_FAILURE);
	}
	//发起连接请求
	if(connect(fd,(struct sockaddr *)&addr,sizeof(addr))){
		ErrExit("connect");
	}
	//执行客户端处理程序
	udp_main(fd,argv[1]);
	//关闭套接字
	close(fd);
	return 0;
}
